import wxpay from '@service.wxpay'
import alipay from '@service.alipay'
import prompt from '@system.prompt'
import { getUserInfo } from '../../api/user/index'
export const onWechatPay = (payInfo) => {
    return new Promise((resolve, reject) => {
        const payType = wxpay.getType()
        const appPayInfo = payInfo.appPayInfo.split('&')
        // return 
        if (payType === 'APP') {
            wxpay.pay({
                //微信 app支付的prepayId
                prepayid: appPayInfo[2],
                extra: {
                    app_id: 'wx03780cf5ebd6efa6',
                    partner_id: appPayInfo[1],
                    package_value: 'Sign=WXPay',
                    nonce_str: appPayInfo[4],
                    time_stamp: appPayInfo[5],
                    order_sign: appPayInfo[6],
                },
                fail: function (data, code) {
                    reject(`app WX PAY failed, code = ${code}`)
                },
                cancel: function (data, code) {
                    reject(`app WX PAY failed, code = ${code}`)
                },
                success: function (data) {
                    console.log('app WX PAY success')
                    // 更新用户信息
                    getUserInfo(true)
                    resolve()
                },
            })
        } else if (payType === 'MWEB') {
            wxpay.pay({
                //微信网页支付的prepayId
                prepayid: payInfo.orderId,
                extra: {
                },
                fail: function (data, code) {
                    console.log(`WX H5 PAY handling fail, code = ${code}`)
                },
                cancel: function () {
                    console.log('WX H5 PAY handling cancel')
                },
                success: function (data) {
                    //H5方式下，支付成功的回调仅仅只是指将订单递交给微信，并不意味着支付已经成功完成
                    console.log('WX H5 PAY handling success')
                    console.log(JSON.stringify(data))
                },
            })
        } else {
            console.log('WX PAY is not avaliable')
        }
    })

}

export const onAliPay = (orderInfo) => {
    // let orderInfo = this.orderInfo
    return new Promise((resolve, reject) => {
        alipay.pay({
            orderInfo,
            callback: function (ret) {
                console.log('handling callback', ret)
                console.log(JSON.stringify(ret))
                if (ret?.resultStatus == '9000') {
                    prompt.showToast({
                        message: '支付成功'
                    })
                    getUserInfo(true)
                    resolve()
                } else {
                    prompt.showToast({
                        message: ret.memo
                    })
                    reject(ret.memo)
                }
            },
            fail: (e) => {
                console.log('支付失败', e)
                reject(e)
            }
        })
    })

}